package homework0711;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class DouDiZhuMain {
    Card card = new Card();
    List<String> poker = new ArrayList<>();

    public static void main(String[] args) {
        DouDiZhuMain douDiZhuMain = new DouDiZhuMain();
        douDiZhuMain.GenerateTheCard();
    }

    //初始化牌堆
    public void GenerateTheCard() {
        for (int i = 0; i < card.getPoint().length; i++) {
            for (int j = 0; j < card.getType().length; j++) {
                poker.add(card.getPoint()[i] + card.getType()[j]);
            }
        }
        poker.add("小王-99");
        poker.add("大王-100");
        StringBuilder str = new StringBuilder();
        for (String item : poker) {
            //分割 - ，截取下标0，(黑、红、梅、方)用于打印
            str.append(item.split("-")[0]).append(" ");
        }
        System.out.println("初始化牌:" + str);
        shufflePoker();
    }

    //洗牌
    public void shufflePoker() {
        Collections.shuffle(poker);
        StringBuilder str = new StringBuilder();
        for (String item : poker) {
            //分割 - ，截取下标0，(黑、红、梅、方)用于打印
            str.append(item.split("-")[0]).append(" ");
        }
        System.out.println("洗牌:" + str);
        sortPoker();
    }

    //理牌
    public void sortPoker() {
        System.out.println("发牌:");
        //玩家一
        String player1Poker = "";
        List<String> player1 = poker.subList(0, 17);//得到玩家一的牌集合
        player1.sort(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                //分割 - ，截取下标1，用于排序，转为Integer，再相减，得到升序
                return Integer.parseInt(o1.split("-")[1]) - Integer.parseInt(o2.split("-")[1]);
            }
        });
        for (String item : player1) {
            //分割 - ，截取下标0，(黑、红、梅、方)用于打印
            player1Poker += item.split("-")[0] + " ";
        }
        System.out.println("玩家一：" + player1Poker);

        //玩家二
        String player2Poker = "";
        List<String> player2 = poker.subList(17, 34);//得到玩家二的牌集合
        player2.sort(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                //分割 - ，截取下标1，用于排序，转为Integer，再相减，得到升序
                return Integer.parseInt(o1.split("-")[1]) - Integer.parseInt(o2.split("-")[1]);
            }
        });
        for (String item : player2) {
            //分割 - ，截取下标0，(黑、红、梅、方)用于打印
            player2Poker += item.split("-")[0] + " ";
        }
        System.out.println("玩家二：" + player2Poker);

        //玩家三
        String player3Poker = "";
        List<String> player3 = poker.subList(34, 51);//得到玩家三的牌集合
        player3.sort(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                //分割 - ，截取下标1，用于排序，转为Integer，再相减，得到升序
                return Integer.parseInt(o1.split("-")[1]) - Integer.parseInt(o2.split("-")[1]);
            }
        });
        for (String item : player3) {
            //分割 - ，截取下标0，(黑、红、梅、方)用于打印
            player3Poker += item.split("-")[0] + " ";
        }
        System.out.println("玩家三：" + player3Poker);

        //底牌
        String cardStr = "";
        List<String> cardPoker = poker.subList(51, 54);//得到底牌
        cardPoker.sort(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                //分割 - ，截取下标1，用于排序，转为Integer，再相减，得到升序
                return Integer.parseInt(o1.split("-")[1]) - Integer.parseInt(o2.split("-")[1]);
            }
        });
        for (String item : cardPoker) {
            //分割 - ，截取下标0，(黑、红、梅、方)用于打印
            cardStr += item.split("-")[0] + " ";
        }
        System.out.println("底牌：" + cardStr);
    }
}
